#set(GOPATH "$ENV{GOPATH}:${CMAKE_CURRENT_BINARY_DIR}/../")
#set(GOPATH "${CMAKE_CURRENT_BINARY_DIR}/../")
#message(${GOPATH})
#file(MAKE_DIRECTORY ${GOPATH})
#export HTTP_PROXY=http://172.25.21.98:8118
#export HTTPS_PROXY=http://172.25.21.98:8118

# go get function
function(ExternalGoProject_Add TARG)
  add_custom_target(${TARG} env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} get ${ARGN})
endfunction(ExternalGoProject_Add)

function(add_go_executable2 NAME)
  file(GLOB GO_SOURCE RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.go")
  add_custom_command(OUTPUT ${OUTPUT_DIR}/.timestamp
    COMMAND env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} build
    -o "${CMAKE_CURRENT_BINARY_DIR}/${NAME}"
    ${CMAKE_GO_FLAGS} ${GO_SOURCE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

  add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_DIR}/.timestamp ${ARGN})
  install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${NAME} DESTINATION bin)
endfunction(add_go_executable2)

function(add_go_executable NAME)
    set(_options APPEND_PATH DESCRIPTORS NOT_INSTALL NOT_BUILD_ALL ARM_BUILD)
    set(_singleargs TARGET_NAME  OUT_DIR OUT_OPTS WORKING_DIRECTORY)
    set(_multiargs GOLIST OPTS TAG_OPTS)
		
	cmake_parse_arguments(go_exe "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")

    #输出路径 
	if (go_exe_OUT_DIR)
	else()
		set(go_exe_OUT_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
	endif()
	
    #目标名称，如果没有设置，使用NAME
    if (go_exe_TARGET_NAME)
    else()
        set(go_exe_TARGET_NAME ${NAME})
    endif()
    
    #工作目录
    if (go_exe_WORKING_DIRECTORY)
        SET(go_exe_WDIR ${go_exe_WORKING_DIRECTORY})
    else()
        SET(go_exe_WDIR ${CMAKE_CURRENT_SOURCE_DIR})
    endif()
    
    # 工作组，不设置默认为ALL
    if (go_exe_NOT_BUILD_ALL)
        SET(go_exe_BG "")
    else()
        SET(go_exe_BG ALL)
    endif()
	if (go_exe_GOLIST)
		#CGO_ENABLED=0 GOOS=linux GOARCH=arm
        
        SET(GOCMD CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} ${CMAKE_Go_COMPILER})

		set(OPTS ${go_exe_OPTS})
		add_custom_target(${NAME} ${go_exe_BG} WORKING_DIRECTORY  ${go_exe_WDIR})
        if (go_exe_TAG_OPTS)
            add_custom_command(TARGET ${NAME}
                    PRE_BUILD
                    #COMMAND  CGO_ENABLED=0 GOOS=linux GOARCH=arm ${CMAKE_Go_COMPILER} 
                    COMMAND ${GOCMD}
                    ARGS build -tags "${go_exe_TAG_OPTS}" -o ${go_exe_OUT_DIR}/${go_exe_TARGET_NAME}  ${go_exe_OPTS}  ${go_exe_GOLIST}
                    WORKING_DIRECTORY ${go_exe_WDIR}
                    COMMENT "[go build ${NAME}]"
            )  
        else()
            add_custom_command(TARGET ${NAME}
                    PRE_BUILD 
                    #COMMAND  CGO_ENABLED=0 GOOS=linux GOARCH=arm ${CMAKE_Go_COMPILER} 
                    COMMAND ${GOCMD}
                    ARGS build -o ${go_exe_OUT_DIR}/${go_exe_TARGET_NAME}  ${go_exe_OPTS}  ${go_exe_GOLIST}
                    WORKING_DIRECTORY ${go_exe_WDIR}
                    COMMENT "[go build ${NAME}]"
            )  
        endif()
        #add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_DIR}/.timestamp ${ARGN})
        #默认为INSTALL，如果设置 NOT_INSTALL 为true 则不install
        if (go_exe_NOT_INSTALL)
        
        else()
            install(PROGRAMS ${go_exe_OUT_DIR}/${go_exe_TARGET_NAME} DESTINATION bin)     
        endif()
	else()
		MESSAGE(STATUS "add_go_executable no GOLIST ")
	endif()

endfunction()



#[[
function(ADD_GO_LIBRARY NAME BUILD_TYPE)
  if(BUILD_TYPE STREQUAL "STATIC")
    set(BUILD_MODE -buildmode=c-archive)
    set(LIB_NAME "lib${NAME}.a")
  else()
    set(BUILD_MODE -buildmode=c-shared)
    if(APPLE)
      set(LIB_NAME "lib${NAME}.dylib")
    else()
      set(LIB_NAME "lib${NAME}.so")
    endif()
  endif()

  file(GLOB GO_SOURCE RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.go")
  add_custom_command(OUTPUT ${OUTPUT_DIR}/.timestamp
    COMMAND env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} build ${BUILD_MODE}
    -o "${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}"
    ${CMAKE_GO_FLAGS} ${GO_SOURCE}
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

  add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_DIR}/.timestamp ${ARGN})

  if(NOT BUILD_TYPE STREQUAL "STATIC")
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME} DESTINATION bin)
  endif()
endfunction(ADD_GO_LIBRARY)
#]]

function(ADD_GO_LIBRARY NAME BUILD_TYPE )
    set(_options )
    set(_singleargs GO_OUT_DIR GO_OUT_HEADER_DIR)
    set(_multiargs GOLIST GOPATH)
	cmake_parse_arguments(golib "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")

	if(golib_BUILD_TYPE STREQUAL "STATIC")
		set(BUILD_MODE -buildmode=c-archive)
		set(LIB_NAME "lib${NAME}.a")
	else()
		set(BUILD_MODE -buildmode=c-shared)
		if(APPLE)
			set(LIB_NAME "lib${NAME}.dylib")
		else()
			set(LIB_NAME "lib${NAME}.so")
		endif()
	endif()

	if (golib_GO_OUT_DIR)
		
	else()
		SET(golib_GO_OUT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
	endif()	
	
	if (golib_GOPATH)	
		foreach(_path ${golib_GOPATH})
			if (_gopath)
				SET(_gopath "${_gopath}:${_path}")
			else()
				SET(_gopath "${_path}")
			endif()			
		endforeach()
	endif()
	
	SET(golib_FULL_LIB_NAME ${golib_GO_OUT_DIR}/${LIB_NAME})
	SET(golib_FULL_LIB_HEADER "${golib_GO_OUT_DIR}/lib${NAME}.h")

	MESSAGE(STATUS "NAME[${NAME}] BUILD_TYPE[${BUILD_TYPE}]")
	MESSAGE(STATUS "GOLIST[${golib_GOLIST}] GO_OUT_DIR[${golib_GO_OUT_DIR}]")
	
	if (golib_GOLIST)
		add_custom_target(${NAME} ALL 
			WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
		
		if (_gopath)
			add_custom_command(TARGET ${NAME}
				COMMAND GOPATH=${_gopath} ${CMAKE_Go_COMPILER} build ${BUILD_MODE} -o ${golib_FULL_LIB_NAME} ${golib_GOLIST}
				)		
		else()
			add_custom_command(TARGET ${NAME}
				COMMAND ${CMAKE_Go_COMPILER} 
					ARGS build ${BUILD_MODE} -o ${golib_FULL_LIB_NAME} ${golib_GOLIST}
				)
		endif()
		
		if (golib_GO_OUT_HEADER_DIR)
			add_custom_command(TARGET ${NAME}
				COMMAND mv ${golib_FULL_LIB_HEADER} ${golib_GO_OUT_HEADER_DIR}/
				)			
		endif()
	else()
		add_custom_command(OUTPUT ${OUTPUT_DIR}/.timestamp
			COMMAND env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} build ${BUILD_MODE}
			-o "${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}"
			${CMAKE_GO_FLAGS} ${GO_SOURCE}
			WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})	
		add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_DIR}/.timestamp ${ARGN})			
	endif()
  

 if(NOT BUILD_TYPE STREQUAL "STATIC")
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME} DESTINATION bin)
  endif()
endfunction(ADD_GO_LIBRARY)




